import Vue from 'vue';
import VueRouter from "vue-router";

//1.注入路由插件
Vue.use(VueRouter)

//2.定义路由
const routes = [
  {
    path: '/',
    redirect: '/login', //重定向到/login
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    component: () => import('../views/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/users',
        name: 'Users',
        component: () => import('../views/user/Users.vue'),
      },
      {
        path: '/roles',
        name: 'Roles',
        component: () => import('../views/role/Roles')
      },
      {
        path: '/menus',
        name: 'Menus',
        component: () => import('../views/menu/Menus')
      },
      {
        path: '/departments',
        name: 'Departments',
        component: () => import('../views/department/Departments')
      },
      {
        path: '/notices',
        name: 'Notices',
        component: () => import('../views/notice/Notices')
      },
      {
        path: '/notices/add',
        name: 'AddNotices',
        component: () => import('../views/notice/AddNotices'),
      },
      {
        path: '/attachments',
        name: 'Attachments',
        component: () => import('../views/other/Attachments'),
      },
      {
        path: '/welcome',
        name: 'Welcome',
        component: () => import('../views/welcome/Welcome'),
      },
      {
        path: '/loginLog',
        name: 'loginLog',
        component: () => import('../views/log/LoginLog'),
      },
      {
        path: '/logs',
        name: 'Logs',
        component: () => import('../views/log/Logs'),
      },
      {
        path: '/swagger',
        name: 'Swagger',
        component: () => import('../views/other/Swagger'),
      },
      {
        path: '/icons',
        name: 'Icons',
        component: () => import('../views/other/Icons'),
      },
      {
        path: '/blog',
        name: 'Blog',
        component: () => import('../views/other/Blog'),
      },
      {
        path: '/druid',
        name: 'Druid',
        component: () => import('../views/other/SQLMonitor'),
      },
      {
        path: '/products',
        name: 'Product',
        component: () => import('../views/product/Product'),
      },
      {
        path: '/productCategorys',
        name: 'ProductCategorys',
        component: () => import('../views/product/ProductCategory'),
      },
      {
        path: '/inStocks',
        name: 'InStocks',
        component: () => import('../views/product/InStocks'),
      },
      {
        path: '/inStocks/addStocks',
        name: 'AddStocks',
        component: () => import('../views/product/AddStocks'),
      },
      {
        path: '/outStocks',
        name: 'OutStocks',
        component: () => import('../views/product/OutStocks'),
      },
      {
        path: '/stocks',
        name: 'Stocks',
        component: () => import('../views/product/Stocks'),
      },
      {
        path: '/suppliers',
        name: 'Suppliers',
        component: () => import('../views/product/Suppliers'),
      },
      {
        path: '/consumers',
        name: 'Consumers',
        component: () => import('../views/product/Consumers'),
      },
      {
        path: '/map',
        name: 'Map',
        component: () => import('../views/health/Map'),
      },
      {
        path: '/health',
        name: 'Health',
        component: () => import('../views/health/Health'),
      },
      {
        path: '/401',
        name: '401',
        component: () => import('../views/401'),
      }
    ]
  },

]

//3.创建router实例
const router = new VueRouter({
  routes
})

import store from '../store'//引入store
//路由导航守卫
router.beforeEach((to, from, next) => {

  const token = window.localStorage.getItem('JWT_TOKEN');
  if (to.path == '/login') {
    if(!token){
      return next();
    }else{
      return next({path: '/home'})
    }
  }

  if(to.path=='/401'){
    return next();
  }

  if (!token) {
    return next('/login');
  }else {
    //判断是否有访问该路径的权限
    const urls =store.state.userInfo.url;
    //如果是管理员
    if(store.state.userInfo.isAdmin){
      return next();
    }else{
      if(urls.indexOf(to.path) > -1){
        //则包含该元素
        window.sessionStorage.setItem("activePath", to.path);
        return next();
      }else{
        return next("/401");
      }
    }
  }
})

//4.导出router实例
export default router
